﻿// 字典
function Infra_Dictionary(){
	// 表格Store
	var gridDicCategoryStore = new Ext.data.Store({
		url: "/InfraDicCategory/GetPlist",
		remoteSort: true,
		reader: new Ext.data.JsonReader({
			root: 'data',
			totalProperty: 'total',
			id: 'Id'
		}, [{
			name: 'Id',
			type: 'string'
		}, {
			name: 'CreateTime',
			type: 'date'
		}, {
			name: 'Name',
			type: 'string'
		}, {
			name: 'TreeCode',
			type: 'string'
		}, {
			name: 'Description',
			type: 'string'
		}]),
		listeners: {
			'update': function (thiz, record, operation) {
				var model = record.data;
				if (operation == Ext.data.Record.EDIT
								&& !record.getChanges().Id) {
					Ext.Ajax.request({
						url: '/InfraDicCategory/SaveOrUpdate',
						params: model,
						success: function (response, opts) {
							var result = Ext.util.JSON
													.decode(response.responseText);
							if (result.success) {
								if (!record.get('Id'))
									record.set('Id', result.id);
								thiz.commitChanges();
							} else {
								JsHelper.ShowError(result.msg);
								thiz.rejectChanges();
							}
						}
					});
				}
			}           
		}
	});
	gridDicCategoryStore.setDefaultSort('CreateTime', 'DESC');

	// 定义Editor
	var cateGridEditor = new Ext.ux.grid.RowEditor();
	// 定义filters
	var filters = new Ext.ux.grid.GridFilters({
		encode: true,
		filters: [{
			type: 'string',
			dataIndex: 'Name'
		}, {
			type: 'string',
			dataIndex: 'TreeCode'
		}, {
			type: 'string',
			dataIndex: 'Description'
		}]
	});

	var CateStoreUpdataFlag = false;
	var DictionargGridInitFlag = false;
	// 定义表格选择方式
	var sm = new Ext.grid.CheckboxSelectionModel({
		singleSelect: true,
		listeners: {
			'rowselect': function (sm, rowIndex, r) {
				if (CateStoreUpdataFlag) {
					CateStoreUpdataFlag = false;
					return;
				}
				var id = sm.getSelected().get("Id");
				//绑定字典
				gridDictionaryStore.removeAll();
				gridDictionaryStore.baseParams["Id"] = sm.getSelected().get("Id");
				gridDictionaryStore.load({
					params: {
						start: 0,
						limit: ALL_PAGESIZE_SETTING
					}
				});
				DictionargGridInitFlag = true;
			},
			'rowdeselect': function (sm, rowIndex, r) {
				gridDictionaryStore.removeAll();
				DictionargGridInitFlag = false;
			}
		}
	});

	var cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), sm, {
		header: "字典名称",
		dataIndex: 'Name',
		sortable: true,
		width: 70 ,
		updateEdit: false,
		editor: {
			xtype: 'textfield',
			allowBlank: false,
			name: 'Name',
			plugins: [Ext.ux.plugins.RemoteValidator],
			rvOptions: {
				url: '/InfraDicCategory/IsFieldExist',
				params: {
					'FieldName': 'Name',
					'Id': "-1"
				}
			}
		}
	}, {
		header: "字典代码",
		dataIndex: 'TreeCode',
		sortable: true,
		updateEdit: false,
		width: 90 ,
		editor: {
			xtype: 'textfield',
			allowBlank: false,
			name: 'TreeCode',
			plugins: [Ext.ux.plugins.RemoteValidator],
			rvOptions: {
				url: '/InfraDicCategory/IsFieldExist',
				params: {
					'FieldName': 'TreeCode',
					'Id': "-1"
				}
			}
		}
	}, {
		header: "描述",
		dataIndex: 'Description',
		sortable: true,
		editor: {
			xtype: 'textfield'
		}
	}]);

	// 定义表格工具栏
	var tbar = [{
		xtype: "tbbutton",
		minWidth: 80,
		ref: '../addBtn',
		text: "添加项",
		cls: "x-btn-text-icon",
		icon: "/Content/icons/add.png",
		handler: function (btn, e) {
			cateGridEditor.stopEditing();
			gridDicCategoryStore.insert(0, new gridDicCategoryStore.recordType);
			dicCategoryGrid.getView().refresh();
			dicCategoryGrid.getSelectionModel().selectRow(0);
			cateGridEditor.startEditing(0);
		}
	}, "-", {
		xtype: "tbbutton",
		ref: '../delBtn',
		minWidth: 80,
		disabled: true,
		text: "删除项",
		cls: "x-btn-text-icon",
		icon: "/Content/icons/delete.png",
		handler: function (btn, e) {
			JsHelper.DelConfirm(function (btn) {
				if (btn == 'yes') {
					var records = dicCategoryGrid.getSelectionModel()
										.getSelections();
					for (var i = 0; i < records.length; i++) {
						Ext.Ajax.request({
							url: '/InfraDicCategory/Delete',
							params: {
								'Id': records[i].get("Id")
							},
							success: function (response, opts) {
								var result = Ext.util.JSON
													.decode(response.responseText);
								if (result.success == false) {
									JsHelper.ShowError(result.msg);
								} else {
									var paramsId = opts.params.Id;
									var r = gridDicCategoryStore
														.getAt(gridDicCategoryStore.find(
																"Id", paramsId));
									if (Ext.isDefined(r)) {
										gridDicCategoryStore.remove(r);
									}
								}
							}
						});

					}
				}
			});
		}
	}, "-", {
		xtype: "tbbutton",
		minWidth: 80,
		text: "双击修改项",
		cls: "x-btn-text-icon",
		icon: "/Content/icons/pencil.png",
		disabled: true
	}];

	// 分页工具栏
	var bbar = new Ext.PagingToolbar({
		displayInfo: true,
		emptyMsg: "没有数据显示",
		displayMsg: "{0}到{1}条，共{2}条",
		store: gridDicCategoryStore,
		pageSize: ALL_PAGESIZE_SETTING,
		plugins: [filters]
	})

	// 定义表格
	var dicCategoryGrid = new Ext.grid.GridPanel({
		region: "west",
		width: 350,
		sm: sm,
		cm: cm,
		enableColumnMove: false,
		plugins: [cateGridEditor, filters],
		stripeRows: true,
		frame: true,
		border: true,
		layout: "fit",
		store: gridDicCategoryStore,
		loadMask: true,
		tbar: tbar,
		bbar: bbar
	});
	dicCategoryGrid.getSelectionModel().on('selectionchange', function (sm) {
		dicCategoryGrid.delBtn.setDisabled(sm.getCount() < 1);
	});


	/////////////
	// 字典列表
	////////////
	
	// 字典 Store
	var gridDictionaryStore = new Ext.data.Store({
		url: "/InfraDictionary/GetPlistCategoryDictionary",
		remoteSort: true,
		reader: new Ext.data.JsonReader({
			root: 'data',
			totalProperty: 'total',
			id: 'Id'
		}, [{
			name: 'Id',
			type: 'string'
		}, {
			name: 'CreateTime',
			type: 'date'
		}, {
			name: 'Name',
			type: 'string'
		}, {
			name: 'Code',
			type: 'string'
		}, {
			name: 'InputCode1',
			type: 'string'
		}, {
			name: 'InputCode2',
			type: 'string'
		}, {
			name: 'InputCode3',
			type: 'string'
		}, {
			name: 'Index',
			type: 'int'
		}, {
			name: 'Description',
			type: 'string'
		}]),
		listeners: {
			'update': function (thiz, record, operation) {
				var model = record.data;
				if (operation == Ext.data.Record.EDIT && !record.getChanges().Id) {
					var cateId = sm.getSelected().get("Id");
					//alert(cateId);
					model['cateId'] = cateId;
					Ext.Ajax.request({
						url: '/InfraDictionary/SaveOrUpdate',
						params: model,
						success: function (response, opts) {
							var result = Ext.util.JSON.decode(response.responseText);
							if (result.success) {
								if (!record.get('Id'))
									record.set('Id', result.id);
								thiz.commitChanges();
							} else {
								JsHelper.ShowError(result.msg);
								thiz.rejectChanges();
							}
						}
					});
				}
			}
		}
	});
	gridDictionaryStore.setDefaultSort('Index', 'ASC');

	// 定义Editor
	var dictGridEditor = new Ext.ux.grid.RowEditor();
	// 定义filters
	var dictFilters = new Ext.ux.grid.GridFilters({
		encode: true,
		filters: [{
			type: 'string',
			dataIndex: 'Name'
		}, {
			type: 'string',
			dataIndex: 'Code'
		}, {
			type: 'string',
			dataIndex: 'Description'
		}]
	});

	// 定义表格列
	var dict_sm = new Ext.grid.CheckboxSelectionModel();
	var dict_cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), dict_sm, {
		header: "参数名称",
		dataIndex: 'Name',
		sortable: true,
		//updateEdit: false,
		editor: {
			xtype: 'textfield',
			allowBlank: false,
			name: 'Name',
			plugins: [Ext.ux.plugins.RemoteValidator],
			rvOptions: {
				url: '/InfraDictionary/IsFieldExist',
				params: {
					'FieldName': 'Name',
					'Id': "-1"
				}
			}
		}
	}, {
		header: "参数代码",
		dataIndex: 'Code',
		sortable: true,
		//updateEdit: false,
		editor: {
			xtype: 'textfield',
			allowBlank: false,
			name: 'Code',
			plugins: [Ext.ux.plugins.RemoteValidator],
			rvOptions: {
				url: '/InfraDictionary/IsFieldExist',
				params: {
					'FieldName': 'Code',
					'Id': "-1"
				}
			}
		}
	}, {
		header: "排序",
		dataIndex: 'Index',
		sortable: true,
		editor: {
			xtype: 'textfield'
		}
	}, {
		header: "助记码1",
		dataIndex: 'InputCode1',
		sortable: true,
		editor: {
			xtype: 'textfield'
		}
	}, {
		header: "助记码2",
		dataIndex: 'InputCode2',
		sortable: true,
		editor: {
			xtype: 'textfield'
		}
	}, {
		header: "描述",
		dataIndex: 'Description',
		sortable: true,
		editor: {
			xtype: 'textfield'
		}
	}]);

	// 定义表格工具栏
	var dict_tbar = [{
		xtype: "tbbutton",
		minWidth: 80,
		ref: '../addBtn',
		text: "添加参数",
		cls: "x-btn-text-icon",
		icon: "/Content/icons/add.png",
		handler: function (btn, e) {
			dictGridEditor.stopEditing();
			gridDictionaryStore.insert(0, new gridDictionaryStore.recordType);
			DictionaryGrid.getView().refresh();
			DictionaryGrid.getSelectionModel().selectRow(0);
			dictGridEditor.startEditing(0);
		}
	}, "-", {
		xtype: "tbbutton",
		ref: '../delBtn',
		minWidth: 80,
		disabled: true,
		text: "删除参数",
		cls: "x-btn-text-icon",
		icon: "/Content/icons/delete.png",
		handler: function (btn, e) {
			JsHelper.DelConfirm(function (btn) {
				if (btn == 'yes') {
					var records = DictionaryGrid.getSelectionModel()
										.getSelections();
					for (var i = 0; i < records.length; i++) {
						Ext.Ajax.request({
							url: '/InfraDictionary/Delete',
							params: {
								'Id': records[i].get("Id")
							},
							success: function (response, opts) {
								var result = Ext.util.JSON
													.decode(response.responseText);
								if (result.success == false) {
									JsHelper.ShowError(result.msg);
								} else {
									var paramsId = opts.params.Id;
									var r = gridDictionaryStore
														.getAt(gridDictionaryStore.find(
																"Id", paramsId));
									if (Ext.isDefined(r)) {
										gridDictionaryStore.remove(r);
									}
								}
							}
						});

					}
				}
			});
		}
	}, "-", {
		xtype: "tbbutton",
		minWidth: 80,
		text: "双击修改参数",
		cls: "x-btn-text-icon",
		icon: "/Content/icons/pencil.png",
		disabled: true
	}];

	// 分页工具栏
	var dict_bbar = new Ext.PagingToolbar({
		displayInfo: true,
		emptyMsg: "没有数据显示",
		displayMsg: "{0}到{1}条，共{2}条",
		store: gridDictionaryStore,
		pageSize: ALL_PAGESIZE_SETTING,
		plugins: [dictFilters]
	})

	// 定义表格
	var DictionaryGrid = new Ext.grid.GridPanel({
		region: "center",
		sm: dict_sm,
		cm: dict_cm,
		enableColumnMove: false,
		plugins: [dictGridEditor, dictFilters],
		stripeRows: true,
		frame: true,
		border: true,
		layout: "fit",
		store: gridDictionaryStore,
		loadMask: true,
		tbar: dict_tbar,
		bbar: dict_bbar
	});
	DictionaryGrid.getSelectionModel().on('selectionchange', function (sm) {
		DictionaryGrid.delBtn.setDisabled(sm.getCount() < 1);
	});

	// 创建框架
	var main = new Ext.Panel({
		border: false,
		layout: 'border',
		items: [dicCategoryGrid, DictionaryGrid]
	});

	// 加载
	JsHelper.ExtTabDoLayout(main);
	// 注意 在ExtTabDoLayout之后Load
	gridDicCategoryStore.load({
		params: {
			start: 0,
			limit: ALL_PAGESIZE_SETTING
		}
	});
};

Infra_Dictionary();
